using MapMaker.VirtualEarth;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MapMaker
{
    public partial class frmDownloadOptions : Form
    {
        public Queue PreviewList;
        public bool PreviewLoaded = false;

        private string saveDirectory;
        private VirtualEarthTileAccess tileAccess;
        private int centerPixelX;
        private int centerPixelY;
        private int levelOfDetail;
        private VirtualEarthTileAccess.MapType mapType;
        private frmProgress progressForm;
        private frmSelectTiles selectTile;
        private frmSelectZoom selectZoom;

        private delegate void addPreviewImageDelegate(PictureBox image);
        private delegate void previewCompleteDelegate();

        public frmDownloadOptions(VirtualEarthTileAccess tileAccess, int centerPixelX, int centerPixelY, int levelOfDetail, VirtualEarthTileAccess.MapType mapType)
        {
            InitializeComponent();
            this.tileAccess = tileAccess;

            this.centerPixelX = centerPixelX;
            this.centerPixelY = centerPixelY;
            this.levelOfDetail = levelOfDetail;
            this.mapType = mapType;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result = dlgBrowse.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                saveDirectory = dlgBrowse.SelectedPath;
                lblSaveDirectory.Text = dlgBrowse.SelectedPath;
            }
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            bool validationError = false;

            if (string.IsNullOrEmpty(txtName.Text))
            {
                lblName.ForeColor = Color.Red;
                validationError = true;
            }

            if (string.IsNullOrEmpty(saveDirectory))
            {
                lblFolder.ForeColor = Color.Red;
                validationError = true;
            }

            if (!validationError)
            {
                tileAccess.SaveDirectory = saveDirectory;

                tileAccess.MapName = txtName.Text;

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnTiles_Click(object sender, EventArgs e)
        {
            if (PreviewList == null)
            {
                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += new DoWorkEventHandler(getPreview);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(previewComplete);

                VirtualEarthTileAccess.GotPreviewImage += new VirtualEarthTileAccess.GotFileHandler(TileAccess_GotPreviewImage);

                progressForm = new frmProgress();
                progressForm.btnCancel.Enabled = false;
                progressForm.LowerBound = 0;
                progressForm.UpperBound = VirtualEarthTileAccess.GetTilePreviewCount(
                    centerPixelX, centerPixelY, levelOfDetail);
                progressForm.Status = "Generating preview...";

                worker.RunWorkerAsync();
                progressForm.ShowDialog(this);
            }

            selectTile = new frmSelectTiles(PreviewList);

            selectTile.Owner = this;

            selectTile.ShowDialog(this);
        }

        private void TileAccess_GotPreviewImage(int numCompleted)
        {
            progressForm.SetProgress(numCompleted);
        }

        private void previewComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            progressForm.Close();
        }

        private void getPreview(object sender, DoWorkEventArgs e)
        {
            PreviewList = Program.TileAccess.GetTilePreviewList(centerPixelX, centerPixelY, levelOfDetail, mapType, true);
        }

        private void frmDownloadOptions_OnFormClosing(object sender, CancelEventArgs e)
        {
            if (selectTile != null)
            {
                selectTile.Dispose();
            }

            if (progressForm != null)
            {
                progressForm.Dispose();
            }

            if (PreviewList != null)
            {
                PreviewList.Clear();
            }

            PreviewList = null;

            // collect garbage from images loading
            GC.Collect();

            this.Dispose();
        }

        private void btnZoom_Click(object sender, EventArgs e)
        {
            selectZoom = new frmSelectZoom(levelOfDetail);
            selectZoom.Owner = this;
            selectZoom.ShowDialog();
        }
    }
}